<script setup lang="ts">
import {
  ElButton,
  ElAvatar,
  ElDivider,
  ElProgress,
  ElImage,
  ElIcon,
  ElLoading,
  ElResult,
  ElDescriptions,
  ElDescriptionsItem,
  ElTooltip,
  ElScrollbar
} from "element-plus";
import { Picture as IconPicture } from "@element-plus/icons-vue";
// import {} from @ElementPlus/

import { useGameStore } from "@/stores/Game";
import { useRankStore } from "@/stores/Rank";

import {
  confirmTheGame,
  getSummoner,
  getHonor,
  getRank,
  getRP,
  getLolBlueEssence,
  getEnvironment,
  getAvatarUrl,
  giveALike,
  lookingForGame,
  oneMoreGame,
} from "./axios/api";

// 仓库
const gameStore = useGameStore();
const rankStore = useRankStore();

// 获取召唤师头像
let getSrcUrl = async function () {
  let res = await getAvatarUrl();

  const img = btoa(
    new Uint8Array(res as unknown as ArrayBuffer).reduce(
      (data, byte) => data + String.fromCharCode(byte),
      ""
    )
  );
  if (
    !img ||
    img ===
      `eyJlcnJvckNvZGUiOiJSUENfRVJST1IiLCJodHRwU3RhdHVzIjo0MDAsImltcGxlbWVudGF0aW9uRGV0YWlscyI6e30sIm1lc3NhZ2UiOiJGYWlsZWQgdG8gc2VydmUgL2xvbC1nYW1lLWRhdGEvYXNzZXRzL3YxL3Byb2ZpbGUtaWNvbnMvdW5kZWZpbmVkLmpwZyJ9`
  ) {
    return undefined;
  } else {
    return "data:image/png;base64," + img;
  }
};

// 更新召唤师信息
let getUser = async function () {
  let data = await getSummoner();
  if (data!.name === undefined) {
    return;
  }
  let { name, level, xpSinceLastLevel, xpUntilNextLevel, infoId } = data as any;

  // 获取蓝色精粹
  let lol_blue_essence = await getLolBlueEssence();
  // 获取点券
  let RP = await getRP();
  // 获取大区
  let environment = await getEnvironment();

  // 荣誉
  // 获取荣誉
  let honor = (await getHonor()) as any;

  // 获取排位信息
  let rank = (await getRank()) as any;

  // 召唤师头像
  let avatarUrl = await getSrcUrl();

  gameStore.$patch((state) => {
    state.user.name = name;
    state.user.level = level;
    state.user.xpSinceLastLevel = xpSinceLastLevel;
    state.user.xpUntilNextLevel = xpUntilNextLevel;
    state.user.RP = RP;
    state.user.lol_blue_essence = lol_blue_essence;
    state.user.environment = environment;
    state.user.infoId = infoId;
    state.user.avatarUrl = avatarUrl as unknown as string;
  });

  rankStore.$patch((state) => {
    // 荣誉
    state.honor.rewardsLocked = honor.rewardsLocked;
    state.honor.honorLevel = honor.honorLevel;
    state.honor.checkpoint = honor.checkpoint;

    // 单双
    state.rank.rankSolo.tier = rank.rankSolo.tier;
    state.rank.rankSolo.division = rank.rankSolo.division;
    state.rank.rankSolo.leaguePoints = rank.rankSolo.leaguePoints;
    state.rank.rankSolo.wins = rank.rankSolo.wins;
    state.rank.rankSolo.losses = rank.rankSolo.losses;

    // 灵活
    state.rank.rankSr.tier = rank.rankSr.tier;
    state.rank.rankSr.division = rank.rankSr.division;
    state.rank.rankSr.leaguePoints = rank.rankSr.leaguePoints;
    state.rank.rankSr.wins = rank.rankSr.wins;
    state.rank.rankSr.losses = rank.rankSr.losses;

    // 云顶
    state.rank.rankTft.tier = rank.rankTft.tier;
    state.rank.rankTft.division = rank.rankTft.division;
    state.rank.rankTft.leaguePoints = rank.rankTft.leaguePoints;
    state.rank.rankTft.wins = rank.rankTft.wins;
    state.rank.rankTft.losses = rank.rankTft.losses;
  });
};

setTimeout(async () => {
  let data = (await getLolBlueEssence()) as any;
  if (data) {
    // 游戏启动
    await getUser();
    gameStore.GameRunState = true;
  } else {
    // 未启动
    gameStore.GameRunState = false;
    let { token, port } = await (window as any).config.getTokenAndPort();
    window.localStorage.setItem("baseURL", `https://127.0.0.1:${port}`);
    window.localStorage.setItem("token", `${token}`);
  }
}, 0);

// 20分钟刷新
let silent = window.localStorage.getItem("silent") === "true" ? true : false;
let timer = 500;
if (silent) {
  timer = 1000 * 60 * 20;
}

// 检查lol是否启动
setInterval(async () => {
  let data = (await getLolBlueEssence()) as any;

  if (data) {
    // 游戏启动
    await getUser();
    // 游戏运行
    gameStore.GameRunState = true;
  } else {
    // 未启动
    gameStore.GameRunState = false;
    let { token, port } = await (window as any).config.getTokenAndPort();
    window.localStorage.setItem("baseURL", `https://127.0.0.1:${port}`);
    window.localStorage.setItem("token", `${token}`);
  }
}, timer);
</script>

<template>
  <el-scrollbar>
    <router-view></router-view>
  </el-scrollbar>
</template>
